//Modified by Reem Sharawy 1/12/2013
public class vrf_ctlr 
{   
    Public Boolean show_NewVisa {get; set;}
    Public Boolean hide_NewVisa {get; set;}
    Public Boolean show_transfer {get; set;}
    Public Boolean show_GovernmentOrFreeZone {get; set;}
    Public Boolean show_FatherHusband {get; set;}
    Public Boolean show_InternalTransfer {get; set;}
    Public Boolean show_renewal {get; set;}
    Public Boolean hide_fields {get; set;}
    Public Boolean show_DateOfEntry {get; set;}
    Public Boolean show_Mobile {get; set;}
    Public Boolean show_WorkPermit {get; set;}
    Public Boolean show_DateOfExit {get; set;}
    Public Boolean show_Approval {get; set;}
    Public Boolean show_VisaAmendment {get; set;}
    Public Boolean show_NewDesignation {get; set;}
    Public Boolean show_NewPassport {get; set;}
    Public Boolean show_LostPassport {get; set;}
    Public Boolean show_ChangeOfNationality {get; set;}
    Public Boolean show_Others {get; set;}
    Public Boolean show_Cancellation {get; set;}
    //Public Boolean show_CancelOption {get; set;}

    
    Public Health_Card_Renewal_Form__c Application {get; set;}
    
    //Public String lastDateOfEntry {get; set;}
    Public String dateOfBirth {get; set;}
    Public String passportIssueDate {get; set;}
    Public String passportExpiryDate {get; set;}
    Public String companyDate {get; set;}
    Public String SponserPassportExpiry {get; set;}
    Public String SponserDate {get; set;}
    Public String SponserResidencyExpiry {get; set;}
    Public String DateOfExit {get; set;}
    Public static Boolean flag = true;
    
    Public Static String WebParam {get; set;}
    public static string stdemo {get; set;}
    
    public static string url = null;
    public string geturl() { return url;}
    
    public static string soapXML = null;
    public string getsoapXML(){return soapXML;}
    
    public static string xml = null;
    public string getxml() { return xml;}
    public void checktest()
    {
        flag = false;
    }
    
   
    public void Render_NewVisa()
    {
        system.debug( 'Render NewVisa : ' + Application.VRF_Visa_Type__c);
        if(Application.VRF_Visa_Type__c == 'New Visa')
        {
            show_NewVisa = true;
            show_Transfer = false;
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;         
            Application.VRF_Transfer_Flag__c = false;
            show_Mobile = false;
            show_DateOfEntry = false;
            show_VisaAmendment = false;
            show_InternalTransfer = false; 
            show_Cancellation = false;
            show_NewDesignation = false;
            show_DateOfExit = false;
            show_NewPassport = false; 
            show_ChangeOfNationality = false; 
            show_Others =  false;
            
            system.debug( 'Render NewVisa : ' + show_NewVisa);
        }       
        else if(Application.VRF_Visa_Type__c == 'Renewal Visa')
        {
            show_NewVisa = false;
            show_Transfer = false;       
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;         
            Application.VRF_Transfer_Flag__c = false; 
            show_Mobile = false;
            show_VisaAmendment = false;
            show_InternalTransfer = false; 
            show_Cancellation = false;
            show_NewDesignation = false;
            show_DateOfExit = false;
            show_DateOfEntry = false;
            show_Mobile = false;
            show_WorkPermit = false; 
            show_NewPassport = false; 
            show_ChangeOfNationality = false; 
            show_Others =  false;
         
            system.debug( 'Render NewVisa : ' + show_VisaAmendment );
        }
        else if(Application.VRF_Visa_Type__c == 'Visa Cancellation')
        {
            show_Cancellation = true;
            show_DateOfExit = false;
            show_DateOfEntry = false;
            show_Mobile = false;
            show_WorkPermit = false;
            show_VisaAmendment = false;
            show_NewVisa = false;
            show_Transfer = false;       
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;         
            Application.VRF_Transfer_Flag__c = false; 
            show_NewDesignation = false;      
            show_NewPassport = false; 
            show_ChangeOfNationality = false; 
            show_Others =  false; 
            show_InternalTransfer = false; 
        }        
        else if(Application.VRF_Visa_Type__c == 'Visa Amendment' )
        {
            show_VisaAmendment = true;
            show_Cancellation = false;
            show_NewVisa = false;
            show_Transfer = false;       
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;         
            Application.VRF_Transfer_Flag__c = false; 
            show_Mobile = false;
            show_WorkPermit = false; 
            show_NewDesignation = false;
            show_NewPassport = false;
            show_ChangeOfNationality = false;
            show_Others = false;
            show_DateOfExit = false;
            
            system.debug( 'Render NewVisa : ' + show_VisaAmendment );
        }
        
    }
    
    public void Render_Approval()
    {//system.debug( 'Render Approval : ' + Application.VRF_Pre_approval_required__c);
        if(Application.VRF_Pre_approval_required__c == true && Application.VRF_Transfer_Flag__c == false){       
            show_Transfer = false;
            show_Mobile = false;
            show_WorkPermit = false;
            show_Approval = false;
            show_DateOfEntry = false;
            show_VisaAmendment = false;
        }
        else if(Application.VRF_Pre_approval_required__c == false )
        {
            show_NewVisa = true;
        }
    }
    public void Render_Transfer()
    {
         if(Application.VRF_Transfer_Flag__c == true)
        {system.debug( 'value ==>Reem');
            show_Transfer = true;
            show_Mobile = false;
            show_DateOfEntry = false;
            show_WorkPermit = false;
            show_VisaAmendment = false;
            show_NewPassport = false;
            show_ChangeOfNationality = false;
            show_Others = false;
            show_NewDesignation = false;
            
        }
        else
        {
            show_Transfer = false;
            show_InternalTransfer = false; 
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;   
            show_NewVisa = true;  
        }     
    }
    public void Render_GovernmentOrFreeZone()
     {
        if(Application.VRF_Transfer_Flag__c == true && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && Application.VRF_Available_In__c != 'Individual is currently sponsored by father/husband/another individual')
        {
            show_GovernmentOrFreeZone = true;
            show_FatherHusband = false;
            show_InternalTransfer = false;
            Application.Sponsor_Type__c = 'Establishment' ;
            show_VisaAmendment = false;
            show_NewDesignation = false;
        }
        else if(Application.VRF_Transfer_Flag__c == true && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && Application.VRF_Available_In__c != 'Internal Transfer' && Application.VRF_Available_In__c != 'Individual is currently sponsored by Dubai Government Entity or a Free zone')
        {
            show_FatherHusband = true;
            Application.Sponsor_Type__c = 'Resident' ; 
            show_InternalTransfer = false;   
            show_GovernmentOrFreeZone = false;
            show_NewDesignation = false;
            show_VisaAmendment = false;
        }
        else if(Application.VRF_Transfer_Flag__c == true && Application.VRF_Available_In__c == 'Internal Transfer' && Application.VRF_Available_In__c != 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && Application.VRF_Available_In__c != 'Individual is currently sponsored by father/husband/another individual')
        {
            show_InternalTransfer = true;
            show_FatherHusband = false;
            show_GovernmentOrFreeZone = false;
            show_NewDesignation = false;
        }
        /*else
        {
             show_GovernmentOrFreeZone = false;
             show_FatherHusband = true;            
             show_InternalTransfer = true;       
        }*/
     }
     
    public void Render_Individual()
    {
        if(Application.VRF_Available_In__c == 'individual currently inside UAE'&& Application.VRF_Pre_approval_required__c == false)
        {
            show_DateOfEntry = true;
            show_Transfer = false;
            show_Mobile = false;
            show_WorkPermit = false; 
            show_VisaAmendment = false;
            show_DateOfExit = false;
            show_NewDesignation = false;
        }
        else if(Application.VRF_Available_In__c == 'individual currently inside UAE'&& Application.VRF_Pre_approval_required__c == true)
        {
            show_DateOfEntry = false;
            show_Transfer = false;
            show_Mobile = false;
            show_WorkPermit = false; 
            show_VisaAmendment = false;
            show_DateOfExit = false;
            show_NewDesignation = false;
        }
        else if(Application.VRF_Visa_Type__c == 'Visa Cancellation' && Application.VRF_Available_In__c == 'individual currently outside UAE')
        {
            show_DateOfExit = true;
            show_DateOfEntry = false;
            show_Mobile = false;
            show_WorkPermit = false;
            show_NewDesignation = false;
        }     
        else if(Application.VRF_Available_In__c == 'individual currently outside UAE' && Application.VRF_Visa_Type__c == 'New Visa'){
            show_Transfer = false;
            show_Mobile = true;
            show_DateOfEntry = false;
            show_WorkPermit = true;
            show_VisaAmendment = false;        
            show_NewDesignation = false;
            show_NewPassport = false;
            show_ChangeOfNationality = false;
            show_Others = false; 
        }
        /*else if(Application.VRF_Available_In__c == 'Visa Amendment' && Application.VRF_Transfer_Flag__c == false && Application.VRF_Pre_approval_required__c == false)
        {
            show_VisaAmendment = true;
        }*/
        else
        {
            show_WorkPermit = false;
            show_DateOfEntry = false;
            show_Mobile = false;
            show_NewDesignation = false;
        }
    }
    
     
     public void Render_NewDesignation()
     {
        //system.debug('The Request Type Selected : ' + Application.request_type__c);
        if(Application.VRF_Visa_Type__c == 'Visa Amendment' && Application.request_type__c == 'New Designation' )
        {
            show_NewDesignation = true;
            show_NewPassport = false;
            show_ChangeOfNationality = false;
            show_Others = false;
        }
        
        else if(Application.request_type__c == 'New Passport' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' &&  Application.request_type__c != 'Others')
        {
            show_NewDesignation = false;
            show_NewPassport = true;
            show_ChangeOfNationality = false;
            show_Others = false;
        }
        else if(Application.request_type__c == 'Lost Passport' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' &&  Application.request_type__c != 'Others' && Application.request_type__c != 'New Passport')
        {
            show_NewDesignation = false;
            show_NewPassport = true;
            show_ChangeOfNationality = false;
            show_Others = false;
        }
        else if(Application.request_type__c == 'Change Of Nationality' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Others' && Application.request_type__c != 'New Passport')
        {
            show_ChangeOfNationality = true;
            show_NewDesignation = false;
            show_NewPassport = false;
            show_Others = false;
        }
        else if(Application.request_type__c == 'Others' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' && Application.request_type__c != 'New Passport')
        {
            show_Others = true;
            show_ChangeOfNationality = false;
            show_NewDesignation = false;
            show_NewPassport = false;
        }
        
     }

    public vrf_ctlr()
    {
        Application = new Health_Card_Renewal_Form__c();
        Application.First_Name__c = 'First';
        Application.Middle_Name__c = 'Middle';
        Application.Last_Name__c = 'Last';
        Application.DRV_Sponser_First_Name__c = 'First';
        Application.DRV_Sponser_Middle_Name__c = 'Middle';
        Application.DRV_Sponser_Last_Name__c = 'Last';
    }
    public List<SelectOption> getSponsorType()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Resident', 'Resident'));
        options.add(new SelectOption('Establishment', 'Establishment'));  
       
        return options;
    }
    public List<SelectOption> getCountries()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Country__c> CountriesList = [SELECT id, Name FROM Country__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Country__c country : CountriesList)
        {
            options.add(new SelectOption(country.id, country.name));
        }
        
        return options;
    }
 
    public List<SelectOption> getCountriesList()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<CountriesList__c> CountriesList = [SELECT id, Name FROM CountriesList__c WHERE IsActive__c = true Order By Name limit 999];
        options.add(new SelectOption('', ''));
        for(CountriesList__c country : CountriesList)
        {
            options.add(new SelectOption(country.id, country.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getCompanies()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Account> CompaniesList = [SELECT id, Name FROM Account Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Account company : CompaniesList)
        {
            options.add(new SelectOption(company.id, company.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getJobTitles()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Occupation__c> OccupationsList = [SELECT id, Arabic_Occupation__c, Name FROM Occupation__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Occupation__c occupation : OccupationsList)
        {
            options.add(new SelectOption(occupation.id, occupation.name));
        }
        
        return options;
    }
        public List<SelectOption> getEmirates()
    { 
        List<SelectOption> options = new List<SelectOption>();
        
        List<Emirates__c> EmiratesList = [SELECT id, Name FROM Emirates__c WHERE Active__c = true Order By Name LIMIT 1000];
        options.add(new SelectOption('', ''));
        for(Emirates__c Emirates : EmiratesList)
        {
            options.add(new SelectOption(Emirates.id, Emirates.name));
            }    
        return options;
    }
    public List<SelectOption> getCity()
    {
    
        List<SelectOption> options = new List<SelectOption>();
        
        List<City__c> CityList = [SELECT id, Name FROM City__c WHERE Active__c = true Order By Name LIMIT 1000];
        options.add(new SelectOption('', ''));
        for(City__c City : CityList)
        {
            options.add(new SelectOption(City.id, City.name));     
        }
        
        return options;
    }
 
    public List<SelectOption> getArea()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Area__c> AreaList = [SELECT id, Name FROM Area__c WHERE Active__c = true Order By Name LIMIT 1000];
        options.add(new SelectOption('', ''));
        for(Area__c Area : AreaList)
        {
            options.add(new SelectOption(Area.id, Area.name));
        }
        
        return options;
    }
    public List<SelectOption> getQualifications()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Qualification__c> QualificationsList = [SELECT id, Name FROM Qualification__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Qualification__c qualification : QualificationsList)
        {
            options.add(new SelectOption(qualification.id, qualification.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getGenders()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Male', 'Male'));
        options.add(new SelectOption('Female', 'Female'));
        
        return options;
    }
            public List<SelectOption> getEstablishmentType()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Government', 'Government'));
        options.add(new SelectOption('Free Zone', 'Free Zone'));       
        
        return options;
    }
    public List<SelectOption> getrelation()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Brother', 'Brother'));
        options.add(new SelectOption('Daughter', 'Daughter'));
        options.add(new SelectOption('Father', 'Father'));
        options.add(new SelectOption('Husband', 'Husband'));
        options.add(new SelectOption('Mom Husband', 'Mom Husband'));
        options.add(new SelectOption('Mother', 'Mother'));
        options.add(new SelectOption('Sister', 'Sister'));
        options.add(new SelectOption('Son', 'Son'));
        options.add(new SelectOption('Unrelated', 'Unrelated'));
        options.add(new SelectOption('Wife', 'Wife'));
        
        return options;
    }
    public List<SelectOption> getSponsorDepartment()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('ABU DHABI', 'ABU DHABI'));
        options.add(new SelectOption('AJMAN', 'AJMAN'));
        options.add(new SelectOption('ALFUJERAH', 'ALFUJERAH'));
        options.add(new SelectOption('DUBAI', 'DUBAI'));
        options.add(new SelectOption('RAS AL KHAIMAH', 'RAS AL KHAIMAH'));
        options.add(new SelectOption('SHARJAH', 'SHARJAH'));
        options.add(new SelectOption('UMM AL QUWAIN', 'UMM AL QUWAIN'));
        
        return options;
    }
    
    public List<SelectOption> getTitles()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Mr', 'Mr'));
        options.add(new SelectOption('Mrs', 'Mrs'));
        options.add(new SelectOption('Ms', 'Ms'));
        
        return options;
    }
    
    public List<SelectOption> getVRFVisaType()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('New Visa', 'New Visa'));
        options.add(new SelectOption('Renewal Visa', 'Renewal Visa'));
        options.add(new SelectOption('Visa Amendment', 'Visa Amendment'));
        options.add(new SelectOption('Visa Cancellation', 'Visa Cancellation'));
        
        return options;
    }
    
    public List<SelectOption> getVRFAvailableIn()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(Application.VRF_Pre_approval_required__c == true)
        {
        options.add(new SelectOption('individual currently inside UAE', 'individual currently inside UAE'));
        
        }else if( Application.VRF_Pre_approval_required__c == false || Application.VRF_Visa_Type__c == 'New Visa'){
        options.add(new SelectOption('individual currently outside UAE', 'individual currently outside UAE'));
        options.add(new SelectOption('individual currently inside UAE', 'individual currently inside UAE'));
        
           return options;
        }
        else if(Application.VRF_Visa_Type__c == 'Visa Cancellation'){
        options.add(new SelectOption('individual currently outside UAE', 'individual currently outside UAE'));
        options.add(new SelectOption('individual currently inside UAE', 'individual currently inside UAE'));
        return options;
        }
        return options;
    }
    public List<SelectOption> getVRFFields()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Individual is currently sponsored by father/husband/another individual', ' Individual is currently sponsored by father/husband/another individual'));
        options.add(new SelectOption('Individual is currently sponsored by Dubai Government Entity or a Free zone', 'Individual  is currently sponsored by Dubai Government Entity or a Free zone'));             
        options.add(new SelectOption('Internal Transfer', 'Internal Transfer'));
        return options;
    }
    public List<SelectOption> getRequestTypes()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('New Passport', 'New Passport'));
        options.add(new SelectOption('New Designation', 'New Designation'));
        options.add(new SelectOption('Lost Passport', 'Lost Passport'));
        options.add(new SelectOption('Change Of Nationality', 'Change Of Nationality'));
        options.add(new SelectOption('Others', 'Others'));
        
        return options;
    }
    
    Public List<SelectOption> getPassportTypes()
    {
        List<Passport_Type__c> pt = [SELECT id, name FROM Passport_Type__c];
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('', ''));
        for(Passport_Type__c pt_trav : pt)
        {
            options.add(new SelectOption(pt_trav.id, pt_trav.name));
        }
        return options;
    }
    
    public List<SelectOption> getHoldUAEPermit()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Yes', 'Yes'));
        options.add(new SelectOption('No', 'No'));
        
        return options;
    }
    
    public List<SelectOption> getMaritalStatuses()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Single', 'Single'));
        options.add(new SelectOption('Married', 'Married'));
        options.add(new SelectOption('Divorced', 'Divorced'));
        options.add(new SelectOption('Widowed', 'Widowed'));
        
        return options;
    }
    
    public List<SelectOption> getReligions()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('', ''));
        options.add(new SelectOption('Bahaei', 'Bahaei'));
        options.add(new SelectOption('Budhist', 'Budhist'));
        options.add(new SelectOption('Christian', 'Christian'));
        options.add(new SelectOption('Hindu', 'Hindu'));
        options.add(new SelectOption('Kadiani', 'Kadiani'));
        options.add(new SelectOption('Muslim', 'Muslim'));
        options.add(new SelectOption('Sikh', 'Sikh'));
        options.add(new SelectOption('Others', 'Others'));
        
        return options;
    }
    
    
    
    Public PageReference ResetApplication()
    {
        Application = new Health_Card_Renewal_Form__c();
        //lastDateOfEntry = '';
        dateOfBirth = '';
        passportIssueDate = '';
        passportExpiryDate = '';
        companyDate = '';
        SponserResidencyExpiry = '';
        SponserDate = '';
        SponserPassportExpiry = '';
        DateOfExit = '';
        return null;
    }
    
    Public PageReference ToMainMenu()
    {
        PageReference pg = new PageReference('/home');
        return pg;
    }
    
    public List<SelectOption> getPortOfEntries()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('', ''));
        options.add(new SelectOption('Abu Dhabi International Airport', 'Abu Dhabi International Airport'));
        options.add(new SelectOption('Al Ain International Airport', 'Al Ain International Airport'));
        options.add(new SelectOption('Dubai International Airport', 'Dubai International Airport'));
        options.add(new SelectOption('Sharjah International Airport', 'Sharjah International Airport'));
        options.add(new SelectOption('Other', 'Other'));
        
        return options;
    }
 
/*    public static Date stringToDate(String s){
      //XML Date String is in the format dd/mm/yyyy
      String[] stringDate = s.split('-');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
}*/
public static Date stringToDate(String s){
      //XML Date String is in the format DD/MM/YYYY
      try{
          String[] stringDate = s.split('/');
          Integer d =  Integer.valueOf(stringDate[0]);
          Integer m = Integer.valueOf(stringDate[1]);
          Integer y = Integer.valueOf(stringDate[2]);
          
          if ( d > 31 || m > 12 || y < 1800){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please make sure all Date values to be in DD/MM/YYYY format.'));
             return null ;
          }
          
          return date.newInstance(y,m,d);
      }catch(System.Exception er){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please make sure all Date values to be in DD/MM/YYYY format.'));
          return null ;
      }    
}
 
    Public PageReference CreateApplication()
    {
        if (Application.VRF_Visa_Type__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose New Visa or Renewal Visa'));       
        }else{
           if (Application.VRF_Visa_Type__c == 'New Visa'){
              if (Application.VRF_Available_In__c == null){
                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Availability'));
              }
              /*else{
                 if (Application.VRF_Available_In__c == 'individual currently inside UAE' && Application.VRF_Pre_approval_required__c == false){
                    if (lastDateOfEntry == null || lastDateOfEntry == ''){
                      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Last Date of Entry'));
                    }else{
                      //try{
                      //   Date.valueof(stringToDate(lastDateOfEntry)).year();
                         Application.VRF_Last_Date_of_Entry__c = stringToDate(lastDateOfEntry);
                      //}catch(System.Exception er){
                      //   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Last Date of Entry should be in a valid Date format.'));
                      //}
                      //Application.VRF_Last_Date_of_Entry__c = stringToDate(lastDateOfEntry);
                    }
                 }
              }*/
           }
         
        }
        if(Application.VRF_Available_In__c == 'individual currently outside UAE' && Application.VRF_Visa_Type__c == 'New Passport')
        {
            if (Application.Hold_UAE_Permit__c == null){
               ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Hold Permit in last 6 Months Yes/No'));
            }
        }
        
        if (Application.VRF_Available_In__c == 'individual currently outside UAE' && Application.VRF_Visa_Type__c == 'New Passport')
            {if (Application.Port_of_Entry__c == null ){
               ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Port of Entry'));
            }
        }
        // Individual is currently sponsored by Dubai Government Entity or a Free zone
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.Sponsor_Type__c == null)){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must choose sponsor type'));
        }
         if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.Establishment_Number__c == null || Application.Establishment_Number__c ==  '')){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter Establishment Number'));
        }
         if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.DRV_Sponser_First_Name__c == null || Application.DRV_Sponser_First_Name__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for  Establishment First Name'));
        }
         if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.DRV_Sponser_Last_Name__c == null || Application.DRV_Sponser_Last_Name__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Establishment Last Name'));
        }
         if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.Commercial_Activity__c == null || Application.Commercial_Activity__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Commercial Activity'));
        }
          if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.Sponsor_Emirates__c == null || Application.Sponsor_Emirates__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponsor Emirates'));
        }
          if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.Establishment_Type__c == null || Application.Establishment_Type__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Establishment Type'));
        }
          if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.Sponsor_Address__c == null || Application.Sponsor_Address__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponsor Address'));
        }
          if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.Transfer_Emirates__c == null)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Emirate in the sponsor details part'));
        }
          if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.Transfer_City__c == null)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for City in the sponsor details part'));
        }
          if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.Residency_Tel__c == null || Application.Residency_Tel__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Residency Telephone in the sponsor details part'));
        }
          if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.DRV_Sponser_Office_Phone__c == null || Application.DRV_Sponser_Office_Phone__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Office Telephone in the sponsor details part'));
        }
          if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.Sponsor_P_O_Box__c == null || Application.Sponsor_P_O_Box__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for P.O.Box in the sponsor details part'));
        }
          if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && (Application.DRV_Sponser_Mobile__c == null || Application.DRV_Sponser_Mobile__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Mobile in the sponsor details part'));
        }
        // if sponsor by father/husband/another individual
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.Sponsor_Type__c == null)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value Sponsor Type'));
        }
       if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.Sponsor_Department__c == null || Application.Sponsor_Department__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value Sponsor Department'));
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.DRV_Sponser_First_Name__c == null || Application.DRV_Sponser_First_Name__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for  Establishment First Name'));
        }
         if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.DRV_Sponser_Last_Name__c == null || Application.DRV_Sponser_Last_Name__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Establishment Last Name'));
        }
         if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.DRV_Sponser_Currently_Nationality__c == null)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Nationality in the sponsor details part'));
        }
         if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.Sponsor_Gender__c == null || Application.Sponsor_Gender__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Gender'));
        }
         if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (SponserDate == null || SponserDate ==  '')){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Date of Birth in the sponsor details part'));     
        } else if  (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (SponserDate != null || SponserDate !=  ''))
        {
         Application.DRV_Sponser_Date__c = stringToDate(SponserDate);
        }
         if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.Residency_Years__c == null || Application.Residency_Years__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Residency Years'));
        }
        if  (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (SponserResidencyExpiry == null || SponserResidencyExpiry ==  '')){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Residency expiry date'));
        } else if  (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (SponserResidencyExpiry != null || SponserResidencyExpiry !=  ''))
        {
         Application.DRV_Sponser_Residency_Expiry__c = stringToDate(SponserResidencyExpiry);
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.DRV_Sponser_Passport_Number__c == null || Application.DRV_Sponser_Passport_Number__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Passport Number in the sponsor details part'));
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (SponserPassportExpiry == null || SponserPassportExpiry ==  '')){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose a value for expiry date in the sponsor details part'));
        } else if  (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (SponserPassportExpiry != null || SponserPassportExpiry !=  ''))
        {
         Application.DRV_Sponser_Passport_Expiry__c = stringToDate(SponserPassportExpiry);
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.Sponsor_Mother_Full_Name__c == null || Application.Sponsor_Mother_Full_Name__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Mother Full Name in the sponsor details part'));
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.Sponsor_Address__c == null || Application.Sponsor_Address__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponsor Address'));
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.Transfer_Emirates__c == null)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Emirate in the sponsor details part'));
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.Transfer_City__c == null)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for City in the sponsor details part'));
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.Residency_Tel__c == null || Application.Residency_Tel__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Residency Telephone in the sponsor details part'));
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.DRV_Sponser_Office_Phone__c == null || Application.DRV_Sponser_Office_Phone__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Office Telephone in the sponsor details part'));
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.Sponsor_P_O_Box__c == null || Application.Sponsor_P_O_Box__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for P.O.Box in the sponsor details part'));
        }
        if (Application.VRF_Transfer_Flag__c == true && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && (Application.DRV_Sponser_Mobile__c == null || Application.DRV_Sponser_Mobile__c ==  '')){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Mobile in the sponsor details part '));
        }
        
        if (Application.VRF_Visa_Type__c == 'Visa Cancellation' && Application.VRF_Available_In__c == 'individual currently outside UAE' && (DateOfExit !=  '' && DateOfExit != null) ) 
        {
           Application.Date_of_exit__c = stringToDate(DateOfExit);
        }
        system.debug( DateOfExit !=  '' );
        system.debug( DateOfExit !=  null);
        system.debug( DateOfExit);
        
        
        //personal details:
        if (Application.Title__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Title'));
        }
        
        if (Application.First_Name__c == 'First' || Application.First_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for First Name'));
        }
        if (Application.Last_Name__c == 'Last' || Application.Last_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Last Name'));
        }
        
        //if (Application.Arabic_Name__c == null || Application.Arabic_Name__c == ''){
        //  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Arabic Name'));
        //}
        
        if (Application.Currently_Nationality__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Currently Nationality'));
        }
        if (Application.Previous_Nationality__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Previous Nationality'));
        }
        
        if (dateOfBirth== null || dateOfBirth==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Date of Birth'));
        }else{
           Application.Date_of_Birth__c = stringToDate(dateOfBirth);
        }
        //if (Application.VRF_Visa_Type__c == 'Visa Cancellation' && Application.VRF_Available_In__c == 'individual currently outside UAE' && (DateOfExit != null || DateOfExit !=  '') ) 
        //{
          // Application.Date_of_exit__c = stringToDate(DateOfExit);
        //}
        if (Application.Country_of_Birth__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Country of Birth'));
        }
        
        if (Application.City_of_Birth__c == null || Application.City_of_Birth__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose City of Birth'));
        }
        if ((Application.VRF_Transfer_Flag__c == true || Application.VRF_Visa_Type__c == 'Renewal Visa' || Application.VRF_Available_In__c == 'individual currently inside UAE') && Application.VRF_Available_In__c != 'individual currently outside UAE' && (Application.Applicant_Mobile_Number__c == null || Application.Applicant_Mobile_Number__c ==  '')){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Provide Applicant Mobile Number'));
        }
        if(Application.Company_Index_Card_Number__c == null || Application.Company_Index_Card_Number__c ==  '')
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Provide Company Index Card Number'));
        }
        /*if (Application.VRF_Available_In__c == 'individual currently outside UAE' && (Application.Company_Point_Of_Contact_Mobile_Number__c == null || Application.Company_Point_Of_Contact_Mobile_Number__c ==  '')){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Provide Company Point Of Contact Mobile Number'));
        }*/
        if (Application.City_of_Birth__c == null || Application.City_of_Birth__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose City of Birth'));
        }
        if (Application.Passport_Number__c == null || Application.Passport_Number__c == '' ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Passport Number'));
        }
        
        if (passportIssueDate == null || passportIssueDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport Issue Date'));
        }else{
           Application.Passport_Issue_Date__c = stringToDate(passportIssueDate);
        }
        
        if (passportExpiryDate == null || passportExpiryDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport Expiry Date'));
        }else{
           Application.Passport_Expiry_Date__c = stringToDate(passportExpiryDate);
        }
        
        if (Application.Passport_Country_of_Issue__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport Country of Issue'));
        }
        
        if (Application.Passport_City_of_Issue__c == null || Application.Passport_City_of_Issue__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport City of Issue'));
        }
        
        if (Application.Mother_Full_Name__c == null || Application.Mother_Full_Name__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Mother Full Name'));
        }
        
        if (Application.Marital_Status__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Marital Status'));
        }
        
        if (Application.Religion__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Religion'));
        }
        
        if (Application.Job_Title_Position__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Job Title/Position'));
        }
        
        if (Application.Education_Qualification__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Education Qualification'));
        }
        
        if (Application.Gender__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Gender'));
        }
        if (Application.address_line_1__c == null || Application.address_line_1__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Address'));
        }
        if (Application.outside_city__c == null || Application.outside_city__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert City outside UAE'));
        }
        if (Application.outside_country__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Country'));
        }
        if (Application.outside_telephone_number__c == null || Application.outside_telephone_number__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Telephone Number'));
        }
        if (Application.Emirates_dropdown__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Emirate'));
        }
        if (Application.City_dropdown__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert City inside UAE'));
        }
        if (Application.P_O_Box__c == null || Application.P_O_Box__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert P.O.Box'));
        }
        if (Application.inside_residence_telephone__c == null || Application.inside_residence_telephone__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Residence Telephone'));
        }
        if (Application.inside_office_telephone__c == null || Application.inside_office_telephone__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Office Telephone'));
        }
        if (Application.applicants_email_address__c == null || Application.applicants_email_address__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Email Address'));
        }
        
        if (Application.Company_Name__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose a Company'));
        } else {
        	Application.Establishment_Name1__c = Application.Company_Name__c;
        }
        if (Application.Name_of_Authorized_Signatory__c == null || Application.Name_of_Authorized_Signatory__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Name of Authorized Signatory'));
        }        
        
        if (companyDate == null || companyDate == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Company Date'));
        }else{
           Application.Date__c = stringToDate(companyDate);
        }
        if (ApexPages.hasMessages()){
            System.debug('It has Some Messages');       
           return null ;
        }
        
       try
        {
            Form_Header__c Header ;
            if(Application.VRF_Visa_Type__c == 'Renewal Visa'){
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'DIFC Sponsorship Form - Renewal'];
            }else if (Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Transfer_Flag__c == true){
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'DIFC Sponsorship Form - Transfer'];
            }else{
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'DIFC Sponsorship Form - New'];
            }
            //Form_Header__c Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'DIFC Business Visit Visa Form'];
            String Webservice_String = '';
            for(Form_Detail__c Detail : Header.Form_Details__r)
            {
                if(Detail.Field_Master__r.API_Name__c == NULL)
                {
                    Webservice_String += '*' + Detail.Tab_Space__c + '|';
                }
                
                    else if(Detail.Source__c == 'Form')
                    {     
                       String FieldValue = '';
                    
                          /**}else if(Detail.Field_Master__r.API_Name__c == 'address_line_1__c'){
                            FieldValue = Application.address_line_1__c ; 
                          }else if(Detail.Field_Master__r.API_Name__c == 'outsid_city__c'){
                            FieldValue = Application.outside_city__c ;   
                          }else if(Detail.Field_Master__r.API_Name__c == 'outside_country__c'){
                            FieldValue = Application.outside_country__c ; 
                          }else if(Detail.Field_Master__r.API_Name__c == 'outside_telephone_number__c'){
                            FieldValue = Application.outside_telephone_number__c ; 
                          }else if(Detail.Field_Master__r.API_Name__c == 'emirates__c'){
                            FieldValue = Application.emirates__c ;   
                          }else if(Detail.Field_Master__r.API_Name__c == 'inside_city__c'){
                            FieldValue = Application.inside_city__c ;
                          }else if(Detail.Field_Master__r.API_Name__c == 'area__c'){
                            FieldValue = Application.area__c ; 
                          }else if(Detail.Field_Master__r.API_Name__c == 'P_O_Box__c'){
                            FieldValue = Application.P_O_Box__c ; 
                          }else if(Detail.Field_Master__r.API_Name__c == 'inside_residence_telephone__c'){
                            FieldValue = Application.inside_residence_telephone__c ;  
                          }else if(Detail.Field_Master__r.API_Name__c == 'inside_office_telephone__c'){
                            FieldValue = Application.inside_office_telephone__c ; 
                          }else if(Detail.Field_Master__r.API_Name__c == 'applicants_email_address__c'){
                            FieldValue = Application.applicants_email_address__c ;    
                          }**/
                          if(Detail.Field_Master__r.API_Name__c == 'First_Name__c'){
                            FieldValue = Application.First_Name__c ;          
                          }else if(Detail.Field_Master__r.API_Name__c == 'Middle_Name__c'){
                            FieldValue = Application.Middle_Name__c ;         
                          }else if(Detail.Field_Master__r.API_Name__c == 'Last_Name__c'){
                            FieldValue = Application.Last_Name__c ;                                            
                          }else if(Detail.Field_Master__r.API_Name__c == 'Gender__c'){
                            FieldValue = Application.Gender__c ;              
                          }else if(Detail.Field_Master__r.API_Name__c == 'Date_of_Birth__c'){
                            FieldValue = string.valueOf(Application.Date_of_Birth__c).split('-', 0)[2] + '/' + string.valueOf(Application.Date_of_Birth__c).split('-', 0)[1] + '/' + string.valueOf(Application.Date_of_Birth__c).split('-', 0)[0];         
                          }else if(Detail.Field_Master__r.API_Name__c == 'City_of_Birth__c'){
                            FieldValue = Application.City_of_Birth__c ;                                                  
                          }else if(Detail.Field_Master__r.API_Name__c == 'Passport_Number__c'){
                            FieldValue = Application.Passport_Number__c ;               
                          }else if(Detail.Field_Master__r.API_Name__c == 'Passport_City_of_Issue__c'){
                            FieldValue = Application.Passport_City_of_Issue__c ;                                                
                          }else if(Detail.Field_Master__r.API_Name__c == 'Passport_Issue_Date__c'){
                            FieldValue = string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[2] + '/' + string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[1] + '/' + string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[0];   
                          }else if(Detail.Field_Master__r.API_Name__c == 'Passport_Expiry_Date__c'){
                            FieldValue = string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[2] + '/' + string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[1] + '/' + string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[0];    
                          }else if(Detail.Field_Master__r.API_Name__c == 'Religion__c'){
                            FieldValue = String.ValueOf(Application.Religion__c)  ;   
                          }else if(Detail.Field_Master__r.API_Name__c == 'Marital_Status__c'){
                            FieldValue = String.ValueOf(Application.Marital_Status__c) ;                                                           
                          }else if(Detail.Field_Master__r.API_Name__c == 'Mother_Full_Name__c'){
                            FieldValue = String.ValueOf(Application.Mother_Full_Name__c) ;    
                          }else if(Detail.Field_Master__r.API_Name__c == 'Applicant_Mobile_Number__c'){
                            if(Application.VRF_Available_In__c == 'individual currently outside UAE')
                            {
                                Application.Applicant_Mobile_Number__c = Application.Company_Point_Of_Contact_Mobile_Number__c;
                            }
                            
                            FieldValue = Application.Applicant_Mobile_Number__c;    
                          }else if(Detail.Field_Master__r.API_Name__c == 'Company_Point_Of_Contact_Mobile_Number__c')
                          {
                            FieldValue = Application.Company_Point_Of_Contact_Mobile_Number__c;
                          }else if(Detail.Field_Master__r.API_Name__c == 'Company_Index_Card_Number__c')
                          {
                            FieldValue = Application.Company_Index_Card_Number__c;
                          }
                          
                          
                        if(Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1) != '')
                        {
                            string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                            Schema.SObjectType stype = gd.get(Strg);
                            sObject tagrcd;
                    
                            tagrcd = stype.newSObject();
                               //PO Orig: if(Detail.Field_Master__r.API_Name__c == 'Establishment_Name1__c' && Application.VRF_Available_In__c == 'Internal Transfer'){
                               if(Detail.Field_Master__r.API_Name__c == 'Establishment_Name1__c'){
                                 //Add Same Validation Checks used in Validation for this Field.
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Establishment_Name1__c + '\'');
                               
                               /**}else if(Detail.Field_Master__r.API_Name__c == 'address_line_1__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.address_line_1__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'outside_city__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.outside_city__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'outside_country__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.outside_country__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'outside_telephone_number__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.outside_telephone_number__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'emirates__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.emirates__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'inside_city__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.inside_city__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'area__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.area__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'P_O_Box__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.P_O_Box__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'inside_residence_telephone__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.inside_residence_telephone__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'inside_office_telephone__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.inside_office_telephone__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'applicants_email_address__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.applicants_email_address__c + '\'');
                               }**/
                               
                                                           
                               }else if(Detail.Field_Master__r.API_Name__c == 'Passport_Type__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Passport_Type__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Currently_Nationality__c + '\'');     
                               }else if(Detail.Field_Master__r.API_Name__c == 'Job_Title_Position__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Job_Title_Position__c + '\'');                                                                                               
                               }else if(Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Country_of_Birth__c + '\'');                                                                                           
                               }else if(Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Passport_Country_of_Issue__c + '\'');                                                                                              
                               }else if(Detail.Field_Master__r.API_Name__c == 'Previous_Nationality__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Previous_Nationality__c + '\'');          
                               }else if(Detail.Field_Master__r.API_Name__c == 'Education_Qualification__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Education_Qualification__c + '\'');       
                               }                                                                                       
                            try
                            {
                              Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';
                            }catch(System.Exception er)
                            { 
                              Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                            }
                        }
                        else
                        {
                            if(FieldValue != null){
                            Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                            }else{Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                        }
                    }
                    else if(Detail.Source__c == 'Constant')
                    {
                        if(Detail.Default_Value__r.Value__c != null){
                        Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                        else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }
            }
                
            Webservice_String += '|' + Header.Barcode_Type__c;
            
            WebParam = Webservice_String;
            GenerateBarcode();
            string tdemo1 = stdemo;
            
            
            
            Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'SAP1'];
            Webservice_String = '';
            System.debug('The SAP Header 1=> ' + Header);
            for(Form_Detail__c Detail : Header.Form_Details__r)
            {
                if(Detail.Field_Master__r.API_Name__c == NULL)
                {
                    Webservice_String += '*' + Detail.Tab_Space__c + '|';
                }
                else if(Detail.Source__c == 'Form')
                    {
                        String FieldValue = '';

                        if(Detail.Field_Master__r.API_Name__c == 'Title__c'){
                          FieldValue = Application.Title__c ;   
                        }else if(Detail.Field_Master__r.API_Name__c == 'Last_Name__c')
                        {
                          System.debug('The Last Name SAP 1'+ Application.Last_Name__c); 
                          FieldValue = Application.Last_Name__c ;  
                        }
                        /**}else if(Detail.Field_Master__r.API_Name__c == 'address_line_1__c'){
                          FieldValue = Application.address_line_1__c ;    
                        }else if(Detail.Field_Master__r.API_Name__c == 'outside_city__c'){
                          FieldValue = Application.outside_city__c ;   
                        }else if(Detail.Field_Master__r.API_Name__c == 'outside_country__c'){
                          FieldValue = Application.outside_country__c ;   
                        }else if(Detail.Field_Master__r.API_Name__c == 'outside_telephone_number__c'){
                          FieldValue = Application.outside_telephone_number__c ; 
                        }else if(Detail.Field_Master__r.API_Name__c == 'emirates__c'){
                          FieldValue = Application.emirates__c ; 
                        }else if(Detail.Field_Master__r.API_Name__c == 'inside_city__c'){
                          FieldValue = Application.inside_city__c ; 
                        }else if(Detail.Field_Master__r.API_Name__c == 'area__c'){
                          FieldValue = Application.area__c ; 
                        }else if(Detail.Field_Master__r.API_Name__c == 'P_O_Box__c'){
                          FieldValue = Application.P_O_Box__c ; 
                        }else if(Detail.Field_Master__r.API_Name__c == 'inside_residence_telephone__c'){
                          FieldValue = Application.inside_residence_telephone__c ; 
                        }else if(Detail.Field_Master__r.API_Name__c == 'inside_office_telephone__c'){
                          FieldValue = Application.inside_office_telephone__c ; 
                        }else if(Detail.Field_Master__r.API_Name__c == 'applicants_email_address__c'){
                          FieldValue = Application.applicants_email_address__c ;                                                                
                        }**/
                        else if(Detail.Field_Master__r.API_Name__c == 'Middle_Name__c')
                        {
                          FieldValue = Application.Middle_Name__c ;   
                          System.debug('The Middle Name for SAP1 => '+ Application.Middle_Name__c);           
                        }
                        
                        else if(Detail.Field_Master__r.API_Name__c == 'First_Name__c')
                        {
                          FieldValue = Application.First_Name__c ;          
                          System.debug('The First Name for SAP1 => '+ Application.First_Name__c);
                        }else if(Detail.Field_Master__r.API_Name__c == 'Gender__c')
                        {
                          FieldValue = Application.Gender__c ;                                       
                          System.debug('The Gender for SAP1 => '+ Application.Gender__c);
                        }else if(Detail.Field_Master__r.API_Name__c == 'Date_of_Birth__c'){
                          if(string.valueOf(Application.Date_of_Birth__c).split('-', 0)[0].Length() > 1){
                              FieldValue = string.valueOf(Application.Date_of_Birth__c).split('-', 0)[2] + '.' + string.valueOf(Application.Date_of_Birth__c).split('-', 0)[1] + '.' + string.valueOf(Application.Date_of_Birth__c).split('-', 0)[0];
                              }else{ FieldValue = string.valueOf(Application.Date_of_Birth__c).split('-', 0)[2] + '.0' + string.valueOf(Application.Date_of_Birth__c).split('-', 0)[1] + '.' + string.valueOf(Application.Date_of_Birth__c).split('-', 0)[0]; }     
                        }else if(Detail.Field_Master__r.API_Name__c == 'Marital_Status__c'){
                          if(Application.Marital_Status__c == 'Single')
                            {
                                FieldValue = Application.Marital_Status__c ;
                            }
                            else if(Application.Marital_Status__c == 'Married')
                            {
                                FieldValue = 'Marr';
                            }
                            else if(Application.Marital_Status__c == 'Widow')
                            {
                                FieldValue = 'Wid';
                            }
                            else if(Application.Marital_Status__c == 'Divorced')
                            {
                                FieldValue = 'Div';
                            }
            
                        }else if(Detail.Field_Master__r.API_Name__c == 'City_of_Birth__c'){
                          FieldValue = Application.City_of_Birth__c ;                                  
                        }else if(Detail.Field_Master__r.API_Name__c == 'Religion__c'){
                          FieldValue = string.valueOf(Application.Religion__c) ;                                        
                        }
                        
                        if(Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1) != '')
                        {
                            string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                            Schema.SObjectType stype = gd.get(Strg);
                            sObject tagrcd;
                    
                            tagrcd = stype.newSObject();                          
                            /**}else if(Detail.Field_Master__r.API_Name__c == 'address_line_1__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.address_line_1__c + '\'');
                            }else if(Detail.Field_Master__r.API_Name__c == 'outside_city__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.outside_city__c + '\'');
                            }else if(Detail.Field_Master__r.API_Name__c == 'outside_country__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.outside_country__c + '\'');
                            }else if(Detail.Field_Master__r.API_Name__c == 'outside_telephone_number__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.outside_telephone_number__c + '\'');
                            }else if(Detail.Field_Master__r.API_Name__c == 'emirates__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.emirates__c + '\'');
                            }else if(Detail.Field_Master__r.API_Name__c == 'inside_city__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.inside_city__c + '\'');
                            }else if(Detail.Field_Master__r.API_Name__c == 'area__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.area__c + '\'');
                            }else if(Detail.Field_Master__r.API_Name__c == 'P_O_Box__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.P_O_Box__c + '\'');
                            }else if(Detail.Field_Master__r.API_Name__c == 'inside_residence_telephone__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.inside_residence_telephone__c + '\'');
                            }else if(Detail.Field_Master__r.API_Name__c == 'inside_office_telephone__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.inside_office_telephone__c + '\'');
                            }else if(Detail.Field_Master__r.API_Name__c == 'applicants_email_address__c'){
                              tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.applicants_email_address__c + '\'');                                                                                      
                            }**/                                                        
                            if(Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c'){
                              tagrcd = Database.Query('SELECT id, SAP_Name__c FROM ' + Strg + ' WHERE id = \'' + Application.Country_of_Birth__c + '\'');                             
                            }else if(Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c'){
                              tagrcd = Database.Query('SELECT id, SAP_Name__c FROM ' + Strg + ' WHERE id = \'' + Application.Currently_Nationality__c + '\'');    
                            }
                            
                            try
                            { if(Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c'){Webservice_String += tagrcd.get('SAP_Name__c') + '*' + Detail.Tab_Space__c + '|';}
                              else {Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';}
                            }catch(System.Exception er)
                            { 
                              Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                            }
                        }
                        else
                        {
                            if(FieldValue != null){
                            Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';}
                            else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                        }
                    }
                    else if(Detail.Source__c == 'Constant')
                    {
                        if(Detail.Default_Value__r.Value__c != null){
                        Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                        else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }   
            }
            System.debug('The SAP 1 Webstring => '+ Webservice_String);
            
            Webservice_String += '|' + Header.Barcode_Type__c + 'Sap1';
            
            WebParam = Webservice_String;
            GenerateBarcode();
            string tdemo2 = stdemo;
            Boolean bm_flag = false;
            stdemo = '';
            if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'individual currently inside UAE' && Application.VRF_Transfer_Flag__c == false)
            {
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'DIFC Sponsorship Form - New - Entry Permit - Inside'];
                bm_flag = true; 
            }
            else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'individual currently outside UAE' && Application.VRF_Transfer_Flag__c != true)
            {
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'DIFC Sponsorship Form - New - Entry Permit - Outside'];
                bm_flag = true;
            }
            else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && Application.VRF_Transfer_Flag__c == true)
            {
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'DIFC Sponsorship Form - Transfer'];
                bm_flag = true;
            }
            else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && Application.VRF_Transfer_Flag__c == true)
            {
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'DIFC Sponsorship Form - Transfer'];
                bm_flag = true;
            }
            string tdemo4 = null;
            Webservice_String = '';
            if(bm_flag)
            {
                for(Form_Detail__c Detail : Header.Form_Details__r)
                {
                    if(Detail.Field_Master__r.API_Name__c == NULL)
                    {
                        Webservice_String += '*' + Detail.Tab_Space__c + '|';
                    }
                        else if(Detail.Source__c == 'Form')
                        {     
                           String FieldValue = '';
    
          
              
   if(Detail.Field_Master__r.API_Name__c == 'Company_Index_Card_Number__c'){
     FieldValue = Application.Company_Index_Card_Number__c;               
   }
   else if(Detail.Field_Master__r.API_Name__c == 'Father_Name__c')
   {
     Application.Father_Name__c = Application.Middle_Name__c;
     FieldValue = Application.Father_Name__c ;                              
   }else if(Detail.Field_Master__r.API_Name__c == 'City_EDNRD__c')
   {
     Application.City_EDNRD__c = Application.City_of_Birth__c;
     FieldValue = Application.City_EDNRD__c;                
   }else if(Detail.Field_Master__r.API_Name__c == 'Address1__c')
   {
     Application.Address1__c = Application.City_of_Birth__c;
     FieldValue = Application.Address1__c;                                                       
}else if(Detail.Field_Master__r.API_Name__c == 'First_Name__c'){
  FieldValue = Application.First_Name__c ;          
}else if(Detail.Field_Master__r.API_Name__c == 'Middle_Name__c'){
  FieldValue = Application.Middle_Name__c ;         
}else if(Detail.Field_Master__r.API_Name__c == 'Last_Name__c'){
  FieldValue = Application.Last_Name__c ;                          
}else if(Detail.Field_Master__r.API_Name__c == 'Gender__c'){
  FieldValue = Application.Gender__c;              
}else if(Detail.Field_Master__r.API_Name__c == 'Date_of_Birth__c'){
  if(Header.Name == 'DIFC Sponsorship Form - New - Entry Permit - Outside'){FieldValue = string.valueOf(Application.Date_of_Birth__c).split('-', 0)[2] + '-' + string.valueOf(Application.Date_of_Birth__c).split('-', 0)[1] + '-' + string.valueOf(Application.Date_of_Birth__c).split('-', 0)[0];}
  else{FieldValue = string.valueOf(Application.Date_of_Birth__c).split('-', 0)[2] + '/' + string.valueOf(Application.Date_of_Birth__c).split('-', 0)[1] + '/' + string.valueOf(Application.Date_of_Birth__c).split('-', 0)[0];}   
}else if(Detail.Field_Master__r.API_Name__c == 'City_of_Birth__c'){
  FieldValue = Application.City_of_Birth__c ;                          
}else if(Detail.Field_Master__r.API_Name__c == 'Passport_Number__c'){
  FieldValue = Application.Passport_Number__c ;                            
}else if(Detail.Field_Master__r.API_Name__c == 'Passport_City_of_Issue__c'){
  FieldValue = Application.Passport_City_of_Issue__c;           
}else if(Detail.Field_Master__r.API_Name__c == 'Passport_Issue_Date__c'){
  if(Header.Name == 'DIFC Sponsorship Form - New - Entry Permit - Outside'){FieldValue = string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[2] + '-' + string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[1] + '-' + string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[0];}
  else{FieldValue = string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[2] + '/' + string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[1] + '/' + string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[0];} 
}else if(Detail.Field_Master__r.API_Name__c == 'Passport_Expiry_Date__c'){
  if(Header.Name == 'DIFC Sponsorship Form - New - Entry Permit - Outside'){FieldValue = string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[2] + '-' + string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[1] + '-' + string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[0];}
  else{FieldValue = string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[2] + '/' + string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[1] + '/' + string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[0];} 
}else if(Detail.Field_Master__r.API_Name__c == 'Religion__c'){
  FieldValue = String.ValueOf(Application.Religion__c)  ;   
}else if(Detail.Field_Master__r.API_Name__c == 'Marital_Status__c'){
  FieldValue = String.ValueOf(Application.Marital_Status__c) ;             
}else if(Detail.Field_Master__r.API_Name__c == 'Mother_Full_Name__c'){
  FieldValue = String.ValueOf(Application.Mother_Full_Name__c) ;               
}else if(Detail.Field_Master__r.API_Name__c == 'Applicant_Mobile_Number__c'){
  FieldValue = Application.Applicant_Mobile_Number__c;                      
}
/**else if(Detail.Field_Master__r.API_Name__c == 'address_line_1__c'){
  FieldValue = Application.address_line_1__c ; 
}else if(Detail.Field_Master__r.API_Name__c == 'outsid_city__c'){
  FieldValue = Application.outside_city__c ;   
}else if(Detail.Field_Master__r.API_Name__c == 'outside_country__c'){
  FieldValue = Application.outside_country__c ; 
}else if(Detail.Field_Master__r.API_Name__c == 'outside_telephone_number__c'){
  FieldValue = Application.outside_telephone_number__c ; 
}else if(Detail.Field_Master__r.API_Name__c == 'emirates__c'){
  FieldValue = Application.emirates__c ;   
}else if(Detail.Field_Master__r.API_Name__c == 'inside_city__c'){
  FieldValue = Application.inside_city__c ;
}else if(Detail.Field_Master__r.API_Name__c == 'area__c'){
  FieldValue = Application.area__c ; 
}else if(Detail.Field_Master__r.API_Name__c == 'P_O_Box__c'){
  FieldValue = Application.P_O_Box__c ; 
}else if(Detail.Field_Master__r.API_Name__c == 'inside_residence_telephone__c'){
  FieldValue = Application.inside_residence_telephone__c ;  
}else if(Detail.Field_Master__r.API_Name__c == 'inside_office_telephone__c'){
  FieldValue = Application.inside_office_telephone__c ; 
}else if(Detail.Field_Master__r.API_Name__c == 'applicants_email_address__c'){
  FieldValue = Application.applicants_email_address__c ;          
}**/


//else if(Detail.Field_Master__r.API_Name__c == 'First_Language__c'){
//  FieldValue = String.ValueOf(Application.First_Language__c) ;      
//}else if(Detail.Field_Master__r.API_Name__c == 'Emirates__c'){
//  FieldValue = String.ValueOf(Application.Emirates__c) ;    
//}else if(Detail.Field_Master__r.API_Name__c == 'City__c'){
//  FieldValue = String.ValueOf(Application.City__c) ;    
//}else if(Detail.Field_Master__r.API_Name__c == 'Area__c'){
//  FieldValue = String.ValueOf(Application.Area__c) ;    
//}else if(Detail.Field_Master__r.API_Name__c == 'Street__c'){
//  FieldValue = String.ValueOf(Application.Street__c) ;      
//}else if(Detail.Field_Master__r.API_Name__c == 'Building__c'){
//  FieldValue = String.ValueOf(Application.Building__c) ;    
//}else if(Detail.Field_Master__r.API_Name__c == 'P_O_Box__c'){
//  FieldValue = String.ValueOf(Application.P_O_Box__c) ;     
//}else if(Detail.Field_Master__r.API_Name__c == 'Residency_Tel__c'){
//  FieldValue = String.ValueOf(Application.Residency_Tel__c) ;   
//}else if(Detail.Field_Master__r.API_Name__c == 'Office_Tel__c'){
//  FieldValue = String.ValueOf(Application.Office_Tel__c) ;  
//}else if(Detail.Field_Master__r.API_Name__c == 'Mobile_No__c'){
//  FieldValue = String.ValueOf(Application.Mobile_No__c) ;                
//}
                        if(Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1) != '')
                        {
                            string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                            Schema.SObjectType stype = gd.get(Strg);
                            sObject tagrcd;
                    
                            tagrcd = stype.newSObject();
                               if(Detail.Field_Master__r.API_Name__c == 'Establishment_Name1__c' && Application.VRF_Available_In__c == 'Internal Transfer'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Establishment_Name1__c + '\'');                             
                               }
                               /**else if(Detail.Field_Master__r.API_Name__c == 'address_line_1__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.address_line_1__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'outside_city__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.outside_city__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'outside_country__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.outside_country__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'outside_telephone_number__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.outside_telephone_number__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'emirates__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.emirates__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'inside_city__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.inside_city__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'area__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.area__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'P_O_Box__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.P_O_Box__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'inside_residence_telephone__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.inside_residence_telephone__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'inside_office_telephone__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.inside_office_telephone__c + '\'');
                               }else if(Detail.Field_Master__r.API_Name__c == 'applicants_email_address__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.applicants_email_address__c + '\'');                                                             
                               }**/
                               else if(Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c')
                               {
                                   if(Application.VRF_Available_In__c == 'individual currently outside UAE')
                                   {
                                       tagrcd = Database.Query('SELECT id, name FROM ' + Strg + ' WHERE id = \'' + Application.Currently_Nationality__c + '\'');
                                   }
                                   else
                                   {
                                       tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Currently_Nationality__c + '\'');
                                   }   
                               }
                               else if(Detail.Field_Master__r.API_Name__c == 'Job_Title_Position__c')
                               {
                                   if(Application.VRF_Available_In__c == 'individual currently outside UAE')
                                   {
                                       tagrcd = Database.Query('SELECT id, name FROM ' + Strg + ' WHERE id = \'' + Application.Job_Title_Position__c + '\'');
                                   }
                                   else
                                   {
                                       tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Job_Title_Position__c + '\'');
                                   }
                               }                             
                               else if(Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c')
                               {
                                   if(Application.VRF_Available_In__c == 'individual currently outside UAE')
                                   {
                                       tagrcd = Database.Query('SELECT id, Name FROM ' + Strg + ' WHERE id = \'' + Application.Country_of_Birth__c + '\'');
                                   }
                                   else
                                   {
                                       tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Country_of_Birth__c + '\'');
                                   }  
                               }                                                           
                               else if(Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c')
                               {
                                   if(Application.VRF_Available_In__c == 'individual currently outside UAE')
                                   {
                                       tagrcd = Database.Query('SELECT id, name FROM ' + Strg + ' WHERE id = \'' + Application.Passport_Country_of_Issue__c + '\'');
                                   }
                                   else
                                   {
                                       tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Passport_Country_of_Issue__c + '\'');
                                   }
                               }
                               else if(Detail.Field_Master__r.API_Name__c == 'Country_EDNRD__c')
                               {
                                   Application.Country_EDNRD__c = Application.Country_of_Birth__c;
                                   if(Application.VRF_Available_In__c == 'individual currently outside UAE')
                                   {
                                       tagrcd = Database.Query('SELECT id, Name FROM ' + Strg + ' WHERE id = \'' + Application.Country_EDNRD__c + '\'');
                                       Application.Country_EDNRD__c = String.ValueOf(tagrcd.get('name'));
                                   }
                                   else
                                   {
                                       tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Country_EDNRD__c + '\'');
                                       Application.Country_EDNRD__c = String.ValueOf(tagrcd.get(Detail.Field_Master__r.Referencing_Value__c));
                                   }
                               }
                               else if(Detail.Field_Master__r.API_Name__c == 'Passport_Issue_Government__c')
                               {
                                   Application.Passport_Issue_Government__c = Application.Passport_Country_of_Issue__c;
                                   if(Application.VRF_Available_In__c == 'individual currently outside UAE')
                                   {
                                       System.debug('The Query => SELECT id, name FROM ' + Strg + ' WHERE id = \'' + Application.Passport_Issue_Government__c + '\'');
                                       tagrcd = Database.Query('SELECT id, name FROM ' + Strg + ' WHERE id = \'' + Application.Passport_Issue_Government__c + '\'');
                                       Application.Passport_Issue_Government__c = String.ValueOf(tagrcd.get('name'));
                                   }
                                   else
                                   {
                                       tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Passport_Issue_Government__c + '\'');
                                       Application.Passport_Issue_Government__c = String.ValueOf(tagrcd.get(Detail.Field_Master__r.Referencing_Value__c));
                                   }               
                               }                            
                               else if(Detail.Field_Master__r.API_Name__c == 'Passport_Type__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Passport_Type__c + '\'');                                                          
                               }
                               else if(Detail.Field_Master__r.API_Name__c == 'Previous_Nationality__c'){
                                 tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Previous_Nationality__c + '\'');          
                               }else if(Detail.Field_Master__r.API_Name__c == 'Education_Qualification__c')
                               {
                                   if(Application.VRF_Available_In__c == 'individual currently outside UAE')
                                   {
                                       tagrcd = Database.Query('SELECT id, name FROM ' + Strg + ' WHERE id = \'' + Application.Education_Qualification__c + '\'');
                                   }
                                   else
                                   {
                                       tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.Education_Qualification__c + '\'');
                                   }       
                               }                                                                                                                     
                            
                            try
                            {
                                IF((Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c' || Detail.Field_Master__r.API_Name__c == 'Job_Title_Position__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c' || Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Country_EDNRD__c' || Detail.Field_Master__r.API_Name__c == 'Passport_Issue_Government__c' || Detail.Field_Master__r.API_Name__c == 'Religion__c' || Detail.Field_Master__r.API_Name__c == 'Marital_Status__c' || Detail.Field_Master__r.API_Name__c == 'Education_Qualification__c' ) && Application.VRF_Available_In__c == 'individual currently outside UAE')
                                {
                                    Webservice_String += tagrcd.get('name') + '*' + Detail.Tab_Space__c + '|';
                                }
                                else
                                {
                                    Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';
                                }
                            }catch(System.Exception er)
                            { 
                              Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                            }
                        }
                        else
                        {
                            if(FieldValue != null){
                                if(Detail.Field_Master__r.API_Name__c == 'Gender__c' && Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c != 'individual currently outside UAE' && Application.VRF_Transfer_Flag__c != true){
                                    if(FieldValue == 'Male'){Webservice_String += 'Male*1|';}
                                    else if(FieldValue == 'Female'){Webservice_String += 'Female*6|';}
                                    
                                }
                                else{
                                Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';}  
                            }
                            else{Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                        }
                    }
                    else if(Detail.Source__c == 'Constant')
                    {
                        if(Detail.Default_Value__r.Value__c != null){
                        Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                        else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }
                }
                
                Webservice_String += '|' + Header.Barcode_Type__c;
                
                WebParam = Webservice_String;
                GenerateBarcode();
                tdemo4 = stdemo;
            }
            Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'SAP2'];
            Webservice_String = '';
            
            for(Form_Detail__c Detail : Header.Form_Details__r)
            {
                if(Detail.Field_Master__r.API_Name__c == NULL)
                {
                    Webservice_String += '*' + Detail.Tab_Space__c + '|';
                }

                else if(Detail.Source__c == 'Form')
                {
                
                    String FieldValue = '';

                    if(Detail.Field_Master__r.API_Name__c == 'Passport_Number__c'){
                      FieldValue = string.valueOf(Application.Passport_Number__c) ; 
                    }else if(Detail.Field_Master__r.API_Name__c == 'Passport_Issue_Date__c'){
                      if(string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[0].Length() > 1){
                          FieldValue = string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[2] + '.' + string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[1] + '.' + string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[0];
                          }else{ FieldValue = string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[2] + '.0' + string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[1] + '.' + string.valueOf(Application.Passport_Issue_Date__c).split('-', 0)[0]; }             
                    }else if(Detail.Field_Master__r.API_Name__c == 'Passport_Expiry_Date__c'){
                        if(string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[0].Length() > 1){
                          FieldValue = string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[2] + '.' + string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[1] + '.' + string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[0];
                          }else{ FieldValue = string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[2] + '.0' + string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[1] + '.' + string.valueOf(Application.Passport_Expiry_Date__c).split('-', 0)[0]; }                 
                    }else if(Detail.Field_Master__r.API_Name__c == 'Passport_City_of_Issue__c'){
                      FieldValue = string.valueOf(Application.Passport_City_of_Issue__c) ;                                                 
                    }
                
                    if(Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1) != '')
                    {
                        string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                        Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                        Schema.SObjectType stype = gd.get(Strg);
                        sObject tagrcd;
                
                        tagrcd = stype.newSObject();
                        if(Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c'){
                          tagrcd = Database.Query('SELECT id, SAP_Name__c FROM ' + Strg + ' WHERE id = \'' + Application.Passport_Country_of_Issue__c + '\'');
                        }
                        
                        try
                        {
                           if(Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c'){ Webservice_String += tagrcd.get('SAP_Name__c') + '*' + Detail.Tab_Space__c + '|';}
                           else {Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';}
                        }catch(System.Exception er)
                        { 
                          Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                        }
                    }
                    else
                    {
                        if(FieldValue != null)
                        {
                        Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                        }else{ Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }
                }
                else if(Detail.Source__c == 'Constant')
                {
                    if(Detail.Default_Value__r.Value__c != null){
                    Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                    else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                }

            }
            
            System.debug('The SAP 2 Webstring => '+ Webservice_String);
            Webservice_String += '|' + Header.Barcode_Type__c + 'Sap2';
            
            WebParam = Webservice_String;
            GenerateBarcode();
            string tdemo3 = stdemo;
            
            insert(Application);
            
            Barcode__c br = new Barcode__c(Image_URL__c = tdemo1, Application__c = Application.id);
            Insert(br);
            
            Barcode__c br2 = new Barcode__c(Image_URL__c = tdemo2, Application__c = Application.id);
            Insert(br2);
            
            Barcode__c br3 = new Barcode__c(Image_URL__c = tdemo3, Application__c = Application.id);
            Insert(br3);
            
            if(tdemo4 != null)
            {
                Barcode__c br4 = new Barcode__c(Image_URL__c = tdemo4, Application__c = Application.id);
                Insert(br4);
            }

            PageReference pg = new PageReference('/apex/vrfPDF?id=' + Application.id);

            return pg;
        }
        catch(System.Exception Exp)
        {
            ApexPages.AddMessages(Exp);
            System.debug('The Error Line Number =>'+ exp.getLineNumber() + ' & The Message => '+ exp.getMessage());
            return null;
        }
    }  
    
    public static void Initializevariables()
    {
        // The webservice url -  Pass in the endpoint to be used using the string url 
        url = 'http://demo.smartpaperbpm.com/difc/soap_server/hellowsdl2.php';  

        soapXML = '<?xml version="1.0" encoding="ISO-8859-1"?>';
        soapXML += '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:servicename">';
        soapXML += '<SOAP-ENV:Body>';
        soapXML += '<DoSomething xmlns="urn:servicename">';
        soapXML += '<sf-parameters xmlns="">';
        soapXML += WebParam.replace(' & ', ' &amp; ') + '</sf-parameters>';
        soapXML += '</DoSomething>';
        soapXML += '</SOAP-ENV:Body>';
        soapXML += '</SOAP-ENV:Envelope>';
        
        
        System.debug('The Request XML => '+ soapXML);
    }
    
    public static void GenerateBarcode()
    {    
        // Instantiate a new http object
        Http h = new Http();
              
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        
        Initializevariables();
        
        req.setBody(soapXML);
        req.setEndpoint(url);
        req.setMethod('POST');
        req.setHeader('Content-length', '510' );
        req.setHeader('Content-Type', 'text/xml; charset=ISO-8859-1');  
        req.setHeader('SOAPAction','"http://demo.smartpaperbpm.com/difc/soap_server/hellowsdl1.php#DoSomething"');
        stdemo = '';
        if(flag)
        {    
            //Commented out By Mustafa Zaidi on 9th of July 2012...(because Amazon Webservice is down)
            /*
            HttpResponse res = h.send(req);
            xml = res.getBody();
            XmlStreamReader reader = res.getXmlStreamReader();
            
            stdemo = readXMLResponse(reader,'Pass');*/
            if(!Test.isRunningTest())
            {
                HttpResponse res = h.send(req);
                xml = res.getBody();
                XmlStreamReader reader = res.getXmlStreamReader();
                
                stdemo = readXMLResponse(reader,'Pass');
                System.debug('The XML Response => '+ stdemo);
            }
        }
    }
    
    public static String readXMLResponse(XmlStreamReader reader, String sxmltag)
    {
        string retValue;
        // Read through the XML
        system.debug(reader.toString());
        while(reader.hasNext()) 
        {
            if (reader.getEventType() == XmlTag.START_ELEMENT) 
            {
                if (reader.getLocalName() == sxmltag) 
                {
                    reader.next();
                    if (reader.getEventType() == XmlTag.characters) 
                    {
                        retValue = reader.getText();    
                    }
                } 
            }
            reader.next();
        } 
        return retValue;
    } 
}